package com.zhkj.txg.module.order.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.BaseResponse
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.order.entity.AddressDetailEntity
import com.zhkj.txg.module.order.entity.AddressDetailResponse
import com.zhkj.txg.module.order.entity.AddressListResponse
import com.zhkj.txg.module.order.model.OrderManager
import com.zhkj.txg.utils.ProvinceEntity
import com.zhkj.txg.utils.ProvinceListUtil
import com.zhkj.txg.utils.ProvinceUIData
import com.zhkj.txg.utils.SelectProvinceEntity
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class AddressViewModel(application: Application) : BaseViewModel(application) {
    private val manager: OrderManager =
        OrderManager()
    val addAddressLiveData = MutableLiveData<HttpResponse<BaseResponse>>()
    private val addressListLiveData = MutableLiveData<HttpResponse<AddressListResponse>>()
    val addressDetailLiveData = MutableLiveData<HttpResponse<AddressDetailResponse>>()
    val deleteAddressLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    // 选中的省市区信息
    val selectProvince: SelectProvinceEntity = SelectProvinceEntity()

    // 所有的省市区编码列表
    var provinceList: List<ProvinceEntity>? = null
        private set

    // 给OptionPickerView提供的数据
    var provinceUIData: ProvinceUIData? = null

    /**
     * 获取收货地址列表
     */
    fun addressList(token: String): MutableLiveData<HttpResponse<AddressListResponse>> {
        val list = manager.addressList(token, ViewModelSubscribeListener(addressListLiveData))
        dispose.add(list)
        return addressListLiveData
    }

    /**
     * 获取收货地址详情
     */
    private fun addressDetail(addressId: Int) {
        val detail = manager.addressDetail(
            AccountManager.getToken(), addressId,
            ViewModelSubscribeListener(addressDetailLiveData)
        )
        dispose.add(detail)
    }

    /**
     * 获取省市区列表,并根据addressId判断是否需要加载数据
     */
    fun getProvinceFromAssets(addressId: Int = 0) {
        val province = Observable.create(ObservableOnSubscribe<List<ProvinceEntity>> {
            val provinceList = ProvinceListUtil.getProvinceList(getApplication())
            it.onNext(provinceList)
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({
            provinceList = it
            provinceUIData = ProvinceListUtil.getProvinceUIList(provinceList!!)
            if (addressId > 0) addressDetail(addressId)
        }, { it.printStackTrace() })
        dispose.add(province)
    }

    /**
     * 添加收货地址
     */
    fun addAddress(address: AddressDetailEntity) {
        val addAddress =
            manager.addAddress(AccountManager.getToken(), address, ViewModelSubscribeListener(addAddressLiveData))
        dispose.add(addAddress)
    }

    /**
     * 删除收货地址
     */
    fun deleteAddress(id: Long) {
        val deleteAddress =
            manager.deleteAddress(AccountManager.getToken(), id, ViewModelSubscribeListener(deleteAddressLiveData))
        dispose.add(deleteAddress)
    }

    /**
     * 根据三个code，获取对应的省市区名称，拼接起来
     */
    fun queryAddressString(provinceCode: Int, cityCode: Int, areaCOde: Int): String = buildString {
        provinceList?.find { it.provinceId == provinceCode }?.also {
            selectProvince.provinceId = it.provinceId
            selectProvince.provinceName = it.provinceName
        }?.cityList?.find { it.cityId == cityCode }?.also {
            selectProvince.cityId = it.cityId
            selectProvince.cityName = it.cityName
        }?.areaList?.find { it.id == areaCOde }?.run {
            selectProvince.areaId = id
            selectProvince.areaName = areaName
            append(selectProvince.provinceName).append(selectProvince.cityName).append(selectProvince.areaName)
        }
    }
}